package com.usian.controller;

import com.usian.feign.ItemServiceFeign;
import com.usian.pojo.TbItem;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/backend/item")
@Api("商品管理接口")
public class ItemController {

    @Autowired
    private ItemServiceFeign itemServiceFeign;


    /**
     * 商品的删除
     *
     * @param itemId
     * @return
     */
    @RequestMapping(value = "/deleteItemById", method = RequestMethod.POST)
    @ApiOperation(value = "商品删除", notes = "根据itemId删除该商品")
    @ApiImplicitParam(name = "itemId", type = "Long", value = "商品名id")
    public Result deleteItem(Long itemId) {
        Integer tbItem1 = itemServiceFeign.deleteTbItem(itemId);
        if (tbItem1 == 3) {
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    /**
     * 根据itemId回显商品信息
     *
     * @param itemId
     * @return
     */
    @RequestMapping(value = "/preUpdateItem",method = RequestMethod.POST)
    @ApiOperation(value = "预更新商品接口", notes = "根据itemId查询该商品信息")
    @ApiImplicitParam(name = "itemId", type = "Long", value = "商品名id")
    public Result preUpdateItem(Long itemId) {
        Map<String, Object> map = itemServiceFeign.preUpdateItem(itemId);
        if (map.size() > 0) {
            return Result.ok(map);
        }
        return Result.error("查无结果");
    }

    /**
     * 修改商品
     */
    @RequestMapping(value = "/updateTbItem",method = RequestMethod.POST)
    @ApiOperation(value = "修改商品", notes = "修改商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbItem", type = "TbItem", value = "商品基本信息"),
            @ApiImplicitParam(name = "desc", type = "String", value = "商品描述"),
            @ApiImplicitParam(name = "itemParams", type = "String", value = "商品规格参数")
    })
    public Result updateTbItem(TbItem tbItem, String desc, String itemParams) {
        Integer tbItem1 = itemServiceFeign.updateTbItem(tbItem, desc, itemParams);
        if (tbItem1 == 3) {
            return Result.ok();
        }
        return Result.error("修改失败");
    }

    /**
     * 添加商品
     */
    @RequestMapping(value = "/insertTbItem",method = RequestMethod.POST)
    @ApiOperation(value = "添加商品", notes = "添加商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbItem", type = "TbItem", value = "商品基本信息"),
            @ApiImplicitParam(name = "desc", type = "String", value = "商品描述"),
            @ApiImplicitParam(name = "itemParams", type = "String", value = "商品规格参数")
    })
    public Result insertTbItem(TbItem tbItem, String desc, String itemParams) {
        Integer insertTbItemNum = itemServiceFeign.insertTbItem(tbItem, desc, itemParams);
        if (insertTbItemNum == 3) {
            return Result.ok();
        }
        return Result.error("添加失败");
    }

    /**
     * 查询商品并分页处理
     *
     * @return
     */
    @RequestMapping(value = "/selectTbItemAllByPage",method = RequestMethod.GET)
    @ApiOperation(value = "查询商品并分页处理", notes = "分页查询商品信息，每页显示10条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", type = "Integer", value = "页码"),
            @ApiImplicitParam(name = "rows", type = "Integer", value = "每页展示条数")
    })
    public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1")
                                                Integer page, @RequestParam(defaultValue = "10") Integer rows) {
        PageResult pageResult = itemServiceFeign.selectTbItemAllByPage(
                page, rows);
        if (pageResult != null && pageResult.getResult().size() > 0) {
            return Result.ok(pageResult);
        }
        return Result.error("查无结果");
    }

    /**
     * 查询商品基本信息
     */
    @RequestMapping(value = "/selectItemInfo",method = RequestMethod.POST)
    @ApiOperation(value = "商品查询", notes = "根据itemId查询该商品")
    @ApiImplicitParam(name = "itemId", type = "Long", value = "商品名id")
    public Result selectItemInfo(Long itemId) {
        TbItem tbItem = itemServiceFeign.selectItemInfo(itemId);
        if (tbItem != null) {
            return Result.ok(tbItem);
        }
        return Result.error("查无结果");
    }
}
